﻿using System;
using System.ComponentModel;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using JDKD.BMCS.Model;
using NutShell.DesignPattern;
using System.Linq;

namespace JDKD.BMCS.Business
{
        public class Channel : INotifyPropertyChanged
        {
                #region 构造函数

                public Channel(ChannelModel model)
                {
                        Model = model;
                        raiseType = (RaiseType)Enum.Parse(typeof(RaiseType), model.RaiseType);
                        shiftType = (ShiftType)Enum.Parse(typeof(ShiftType), model.ShiftType);
                }

                #endregion 构造函数

                public static ImageSource RedImageSource = new BitmapImage(new Uri("pack://application:,,,/Images/RedCircle.png"));

                public static ImageSource GreenImageSource = new BitmapImage(new Uri("pack://application:,,,/Images/GreenCircle.png"));

                public static ImageSource BlueImageSource = new BitmapImage(new Uri("pack://application:,,,/Images/BlueCircle.png"));

                public static ImageSource OrangeImageSource = new BitmapImage(new Uri("pack://application:,,,/Images/OrangeCircle.png"));

                public int Id
                {
                        get
                        {
                                return Model.Id;
                        }
                }

                private RaiseType raiseType;

                public RaiseType RaiseType
                {
                        get
                        {
                                return raiseType;
                        }
                        set
                        {
                                raiseType = value;
                                OnPropertyChanged("RaiseImageSource");
                        }
                }

                private ShiftType shiftType;

                public ShiftType ShiftType
                {
                        get
                        {
                                return shiftType;
                        }
                        set
                        {
                                shiftType = value;
                                OnPropertyChanged("ShiftImageSource");
                        }
                }

                public ChannelModel Model { get; private set; }

                private int shiftingCounter = 0;

                public int ShiftingCounter
                {
                        get
                        {
                                return shiftingCounter;
                        }
                        set
                        {
                                shiftingCounter = value;

                                if (ShiftState != Business.ShiftState.已翻 && shiftingCounter > 0)
                                {
                                        ShiftState = Business.ShiftState.已翻;
                                }

                                //10秒
                                if (ShiftState != Business.ShiftState.未翻 && shiftingCounter > 20)
                                {
                                        ShiftState = Business.ShiftState.未翻;
                                        Singleton<BusinessCache>.Instance.ShiftOperations.Add(new ShiftOperation(Id, false));
                                        
                                }
                        }
                }

                private ShiftState shiftState = ShiftState.未翻;

                public ShiftState ShiftState
                {
                        get
                        {
                                return shiftState;
                        }
                        set
                        {
                                shiftState = value;
                                switch (shiftState)
                                {
                                        case Business.ShiftState.未翻:
                                                ShiftingCounter = 0;
                                                break;

                                        case Business.ShiftState.待翻:
                                                break;

                                        case Business.ShiftState.已翻:
                                                ShiftingCounter = 1;
                                                break;
                                }

                                OnPropertyChanged("ShiftImageSource");
                        }
                }

                public ImageSource ShiftImageSource
                {
                        get
                        {
                                switch (ShiftType)
                                {
                                        case Business.ShiftType.手动:
                                                return Channel.RedImageSource;

                                        case Business.ShiftType.自动:
                                                switch (ShiftState)
                                                {
                                                        case Business.ShiftState.未翻:
                                                                return Channel.GreenImageSource;

                                                        case Business.ShiftState.待翻:
                                                                return Channel.OrangeImageSource;

                                                        case Business.ShiftState.已翻:
                                                                return Channel.BlueImageSource;

                                                        default: throw new ArgumentOutOfRangeException();
                                                }

                                        default: throw new ArgumentOutOfRangeException();
                                }
                        }
                }

                

                private bool isRaised = false;

                public bool IsRaised
                {
                        get
                        {
                                return isRaised;
                        }
                        set
                        {
                                isRaised = value;
                                OnPropertyChanged("RaiseImageSource");
                        }
                }

                public ImageSource RaiseImageSource
                {
                        get
                        {
                                switch (RaiseType)
                                {
                                        case Business.RaiseType.手动:
                                                return Channel.RedImageSource;

                                        case Business.RaiseType.自动:
                                                if (IsRaised)
                                                {
                                                        return Channel.BlueImageSource;
                                                }
                                                else
                                                {
                                                        return Channel.GreenImageSource;
                                                }

                                        default: throw new ArgumentOutOfRangeException();
                                }
                        }
                }

                

                private Group group = null;

                public Group Group
                {
                        get
                        {
                                if (group == null)
                                {
                                        if (Id < 5)
                                        {
                                                group = Singleton<BusinessCache>.Instance.Groups[1];
                                        }
                                        else
                                        {
                                                group = Singleton<BusinessCache>.Instance.Groups[2];
                                        }
                                }
                                return group;
                        }
                }

                

                private Area frontArea = null;

                public Area FrontArea
                {
                        get
                        {
                                if (frontArea == null)
                                {
                                        frontArea = Singleton<BusinessCache>.Instance.Aeras.Where(i => i.ChannelId == Id && i.IsFront).Single();
                                }

                                return frontArea;
                        }
                }

                private Area behindArea = null;

                public Area BehindArea
                {
                        get
                        {
                                if (behindArea == null)
                                {
                                        behindArea = Singleton<BusinessCache>.Instance.Aeras.Where(i => i.ChannelId == Id && !i.IsFront).Single();
                                }

                                return behindArea;
                        }
                }

                #region 事件

                public event PropertyChangedEventHandler PropertyChanged;

                #endregion 事件

                #region 方法

                public void OnPropertyChanged(string propertyName)
                {
                        if (PropertyChanged != null)
                        {
                                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                        }
                }

                public void Analyse()
                {
                        FrontArea.Analyse();
                        BehindArea.Analyse();

                        switch(RaiseType)
                        {
                                case Business.RaiseType.手动:
                                        switch (ShiftType)
                                        {
                                                case Business.ShiftType.自动:
                                                        if (BehindArea.IsFull && ShiftState == Business.ShiftState.未翻)
                                                        {
                                                                ShiftState = ShiftState.待翻;
                                                                Singleton<BusinessCache>.Instance.ShiftOperations.Add(new ShiftOperation(Id, true));
                                                        }
                                                        

                                                        break;

                                                default:
                                                        break;
                                        }
                                        break;

                                case Business.RaiseType.自动:
                                        switch (ShiftType)
                                        {
                                                case Business.ShiftType.手动:
                                                        if (BehindArea.IsFull && !IsRaised)
                                                        {
                                                                IsRaised = true;
                                                                new RaiseOperation(Id, true).Run();
                                                        }
                                                        break;

                                                case Business.ShiftType.自动:
                                                        if (BehindArea.IsFull && !IsRaised)
                                                        {
                                                                IsRaised = true;
                                                                new RaiseOperation(Id, true).Run();
                                                        }

                                                        if (IsRaised && FrontArea.IsFull)
                                                        {
                                                                ShiftState = ShiftState.待翻;
                                                                Singleton<BusinessCache>.Instance.ShiftOperations.Add(new ShiftOperation(Id, true));
                                                        }
                                                        break;

                                                default:
                                                        break;
                                        }
                                        break;

                                default: break;
                        }
                }

                #endregion 方法
        }
}